package com.radiobattletoads.player;

import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceChangeListener;
import android.util.Log;
import android.view.WindowManager;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;

public class RadioBattletoadsPreferencesActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
        
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(RadioBattletoadsActivity.instantiatedActivity);
        prefs.registerOnSharedPreferenceChangeListener(this);
        
    }
	
	static public void reloadPreferences(){
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(RadioBattletoadsActivity.instantiatedActivity);
		/* Keep screen on */
        if(preferences.getBoolean("screenon", false)==true){
        	Log.d("preferences","Keeping screen on");
        	RadioBattletoadsActivity.instantiatedActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
        else{
        	Log.d("preferences","NOT keeping screen on");
        	RadioBattletoadsActivity.instantiatedActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
        /* Warnings related to mobile connections */
        if(preferences.getBoolean("warnconnection", true)==true){
        	Log.d("preferences","Warn about mobile connections");
        	RadioBattletoadsActivity.warn_mobile = true;
        }
        else{
        	Log.d("preferences","NOT warn about mobile connections");
        	RadioBattletoadsActivity.warn_mobile = false;
        }
		
	}

	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		RadioBattletoadsPreferencesActivity.reloadPreferences();
		
	}

}
